package ouyang.echoserver;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import miui.xmpp.XmppServer;
import miui.xmpp.XmppServerFactory;
import miui.xmpp.XmppServerListener;
import miui.xmpp.message.Iq;
import miui.xmpp.message.Message;

public class MainActivity extends AppCompatActivity implements XmppServerListener {

    private static final String TAG = "MainActivity";
    //    private EchoServer echoServer;
    private XmppServer server;
    private String remoteIp = null;
    private int remotePort = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        echoServer = new EchoServer();
        server = XmppServerFactory.create(this, 8080);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onButtonStart(View button) {

        new StartTask().execute();
    }

    public void onButtonStop(View button) {
        new StopTask().execute();
    }

    public void onButtonSend(View button) {
        String message = "hi\n";
        server.send(remoteIp, remotePort, message);
    }

    @Override
    public void onAccept(XmppServer server, String ip, int port) {
        Log.d(TAG, String.format("onAccept: %s:%d", ip, port));
        remoteIp = ip;
        remotePort = port;
    }

    @Override
    public void onConnectionClosed(XmppServer server, String ip, int port) {
        Log.d(TAG, String.format("onConnectionClosed: %s:%d", ip, port));
    }

    @Override
    public void onReceived(XmppServer server, String ip, int port, Iq iq) {
        Log.d(TAG, String.format("onReceived: %s:%d", ip, port));
        Log.d(TAG, iq.toString());
    }

    @Override
    public void onReceived(XmppServer server, String ip, int port, Message message) {
        Log.d(TAG, String.format("onReceived: %s:%d", ip, port));
        Log.d(TAG, message.toString());
    }

    private class StartTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            return server.start();
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }

    private class StopTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            return server.stop();
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }
}
